package com.eimf.saf.security.filter;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.eimf.saf.base.entity.BaseEntity;
import com.eimf.saf.security.entity.Staff;
import com.eimf.saf.security.util.AppContant;

/**
 * @author eden
 * @date 2013-11-19 下午4:13:38
 * @Description: 新增修改添加人员和时间信息到实体类中
 */
@Component
public class StaffInfoAspect
{
    // 扫描路径访问规则，对所有add**的方法进行代理写入
    // 所有需要代理写入的方法必须继承BaseEntity，然后强制写入相关用户信息
    public void appendCreateInfo(final BaseEntity baseEntity) throws Exception
    {
        final HttpSession session = ServletActionContext.getRequest().getSession();
        if (session != null)
        {
            final Staff staff = (Staff) session.getAttribute(AppContant.LOGIN_STAFF_INFO);
            baseEntity.setCreatorId(staff.getStaffId());
            baseEntity.setCreateDate(new Date(System.currentTimeMillis()));
        } else
        {
            throw new Exception("Session不存在，系统不能正常工作!");
        }

    }

    public void appendUPdateInfo(final BaseEntity baseEntity) throws Exception
    {
        final HttpSession session = ServletActionContext.getRequest().getSession();
        if (null != session)
        {
            final Staff staff = (Staff) session.getAttribute(AppContant.LOGIN_STAFF_INFO);
            baseEntity.setLastModifyId(staff.getStaffId());
            baseEntity.setLastModifyDate(new Date(System.currentTimeMillis()));
        } else
        {
            throw new Exception("Session不存在，系统不能正常工作!");
        }
    }
}
